package com.ymm.driver.core.facade;

import com.ymm.driver.api.constants.Entity.NoticeEntity;
import com.ymm.driver.api.facade.DriverNoticeService;
import com.ymm.driver.api.utils.Result;
import com.ymm.driver.api.utils.ResultCodeEnum;
import com.ymm.driver.dao.mapper.DriverNoticeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service(version = "server")
@Slf4j
public class DriverNoticeServiceImpl implements DriverNoticeService {
    @Resource
    DriverNoticeMapper driverNoticeMapper;

    /**
     * 保存（投递）通知
     *
     * @param notice 通知实体
     * @return 是否成功
     */
    @Override
    public Result<Boolean> saveNotice(NoticeEntity notice) {
        if (Objects.isNull(notice)) {
            return Result.fail(false, ResultCodeEnum.PARAM_ERROR);
        }
        notice.setRead(false);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        try {
            driverNoticeMapper.add(notice);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.FAIL);
        }
        return Result.success(true);
    }

    /**
     * 读取单个通知
     *
     * @param noticeId 通知Id
     * @return 通知实体
     */
    @Override
    public Result<NoticeEntity> readNotice(Long noticeId, Long driverId) {
        if (Objects.isNull(noticeId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        NoticeEntity notice;
        try {
            notice = driverNoticeMapper.getByNoticeId(noticeId);
            if (Objects.isNull(notice)) {
                return Result.fail(null, ResultCodeEnum.FAIL);
            }
            if (!notice.getDriverId().equals(driverId)) {
                return Result.fail(null, ResultCodeEnum.FAIL);
            }
            //设为已读状态
            driverNoticeMapper.setReadStatus(noticeId, true);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.SERVICE_ERROR);
        }
        return Result.success(notice);
    }

    /**
     * 统计所有未读通知的数量
     *
     * @param driverId 货主Id
     * @return 所有未读通知的数量
     */
    @Override
    public Result<Integer> countNoticeUnread(Long driverId) {
        if (Objects.isNull(driverId)) {
            return Result.fail(null);
        }
        int count;
        try {
            count = driverNoticeMapper.countNoticeUnread(driverId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.SERVICE_ERROR);
        }
        return Result.success(count);
    }

    /**
     * 根据通知类型统计未读通知的数量
     *
     * @param driverId  货主Id
     * @param noticeType 通知类型
     * @return 根据通知类型统计未读通知的数量
     */
    @Override
    public Result<Integer> countNoticeUnreadByNoticeType(Long driverId, Integer noticeType) {
        if (Objects.isNull(driverId) || Objects.isNull(noticeType)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        int count;
        try {
            count = driverNoticeMapper.countNoticeUnreadByNoticeType(driverId, noticeType);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.SERVICE_ERROR);
        }
        return Result.success(count);
    }

//    @Override
//    public Result<List<Notice>> batchReadNotices(List<Integer> noticeIds) {
//        if (Objects.isNull(noticeIds)) {
//            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
//        }
//        List<Notice> notices;
//        try {
//            notices = driverNoticeMapper.listNoticesByNoticeIds(noticeIds);
//            driverNoticeMapper.batchSetReadStatus(noticeIds, true);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("错误");
//            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), ResultCodeEnum.DATABASE_ERROR.getMessage());
//        }
//        return Result.success(notices);
//    }

    @Override
    public Result<List<NoticeEntity>> listAllNotices(Long driverId) {
        if (Objects.isNull(driverId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        List<NoticeEntity> notices;
        try {
            notices = driverNoticeMapper.listNoticesByDriverId(driverId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.SERVICE_ERROR);
        }
        return Result.success(notices);
    }

    @Override
    public Result<List<NoticeEntity>> listNoticesByNoticeType(Long driverId, Integer noticeType) {
        if (Objects.isNull(driverId) || Objects.isNull(noticeType)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        List<NoticeEntity> notices;
        try {
            notices = driverNoticeMapper.listNoticesByDriverIdAndNoticeType(driverId, noticeType);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.SERVICE_ERROR);
        }
        return Result.success(notices);
    }
}
